package org.forever.view
{
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import mx.controls.Alert;
	import mx.core.UIComponent;
	
	import spark.components.BorderContainer;

	public class SequenceFlow extends UIComponent
	{ 
		private var _radius:int=6;
		private var _fromPoint:Point;
		private var _toPoint:Point;
		private var _lineColor:uint=0x000000;
		private var _needArrow:Boolean=false;
		private var _distance:Number;
		private var _rect_w:Number = 6; 
		private var _endRect:BorderContainer;
		private var _startRect:BorderContainer;
		private var _centerRect:BorderContainer;
		
		
		public function SequenceFlow()
		{
			_endRect = new BorderContainer();
			_endRect.width = _rect_w;
			_endRect.height = _rect_w;
			_endRect.visible = true;
			_endRect.setStyle("borderColor","#70B2EE");
			_endRect.addEventListener(MouseEvent.CLICK,endRectClickHandler);
			
			_startRect = new BorderContainer();
			_startRect.width = _rect_w;
			_startRect.height = _rect_w;
			_startRect.visible = true;
			_startRect.setStyle("borderColor","#70B2EE");
			_startRect.addEventListener(MouseEvent.CLICK,startRectClickHandler);
			
			_centerRect = new BorderContainer();
			_centerRect.width = _rect_w;
			_centerRect.height = _rect_w;
			_centerRect.visible = true;
			_centerRect.setStyle("borderColor","#70B2EE");
			_centerRect.addEventListener(MouseEvent.CLICK,centerRectClickHandler);
			
			
			addChild(_endRect);
			addChild(_startRect);
			addChild(_centerRect);
			addEventListener(MouseEvent.CLICK,clickHandler);
		}
		
		public function centerRectClickHandler(e:MouseEvent):void{
			e.stopImmediatePropagation();
			Alert.show("centerRectClickHandler");
		}
		
		public function startRectClickHandler(e:MouseEvent):void{
			e.stopImmediatePropagation();
			Alert.show("startRectClickHandler");
		}
		
		public function endRectClickHandler(e:MouseEvent):void{
			e.stopImmediatePropagation();
			Alert.show("endRectClickHandler");
		}
		
		public function clickHandler(e:MouseEvent):void{
			Alert.show("2");
		}
		
		private function getAngle():int
		{
			var  tmpx:int=_toPoint.x-_fromPoint.x ;
			var tmpy:int=_fromPoint.y -_toPoint.y ;
			var angle:int= Math.atan2(tmpy,tmpx)*(180/Math.PI);
			return angle;
		}
		
		/**绘制线条*/
		public function drawLine():void{
			this.graphics.clear();
			this.graphics.lineStyle(2,_lineColor,1);
			this.graphics.moveTo(_fromPoint.x,_fromPoint.y);
			this.graphics.lineTo(_toPoint.x,_toPoint.y);
		}
		
		/**绘制箭头*/
		public function drawArrow():void{
			var angle:int =getAngle();
			var centerX:int=_toPoint.x-_radius * Math.cos(angle *(Math.PI/180)) ;
			var centerY:int=_toPoint.y+_radius * Math.sin(angle *(Math.PI/180)) ;
			var topX:int=_toPoint.x ;
			var topY:int=_toPoint.y  ;
			
			var leftX:int=centerX + _radius * Math.cos((angle +120) *(Math.PI/180))  ;
			var leftY:int=centerY - _radius * Math.sin((angle +120) *(Math.PI/180))  ;
			
			var rightX:int=centerX + _radius * Math.cos((angle +240) *(Math.PI/180))  ;
			var rightY:int=centerY - _radius * Math.sin((angle +240) *(Math.PI/180))  ;
			
			this.graphics.beginFill(_lineColor,1);
			this.graphics.lineStyle(1,_lineColor,1);
			this.graphics.moveTo(topX,topY);
			this.graphics.lineTo(leftX,leftY);
			
			this.graphics.lineTo(centerX,centerY);
			
			this.graphics.lineTo(rightX,rightY);
			this.graphics.lineTo(topX,topY);
			
			this.graphics.endFill();
		}
		
		/**绘制图形*/
		public function draw():void
		{
			drawLine();
			if(_needArrow)
			{
				drawArrow();
			}
			updateEndRect();
			updateStartRect();
			updateCenterRect();
		}
		
		public function updateEndRect():void{
			_endRect.x = _fromPoint.x - _endRect.width;
			_endRect.y = _fromPoint.y - _endRect.height/2;
		}
		
		public function updateStartRect():void{
			_startRect.x = _toPoint.x;
			_startRect.y = _toPoint.y - _startRect.height/2;
		}
		
		public function updateCenterRect():void{
			_centerRect.x = (_endRect.x + _startRect.x)/2;
			_centerRect.y = (_endRect.y + _endRect.y)/2;
				
		}
		
		/**获取两个矩形中心之间的距离*/
		public function getDistance(first:BorderContainer,second:BorderContainer):Number{
			
			
			return 0;
		}
		
		/******************* get and set ******************/
		
		/**两点之间的距离*/
		public function get distance():Number
		{
			return _distance;
		}

		/**
		 * @private
		 */
		public function set distance(value:Number):void
		{
			_distance = value;
		}

		/**线的颜色*/
		public function get lineColor():uint
		{
			return _lineColor;
		}

		/**
		 * @private
		 */
		public function set lineColor(value:uint):void
		{
			_lineColor = value;
		}

		/**是否需要画箭头*/
		public function get needArrow():Boolean
		{
			return _needArrow;
		}

		/**
		 * @private
		 */
		public function set needArrow(value:Boolean):void
		{
			_needArrow = value;
		}

		/**线终点*/
		public function get toPoint():Point
		{
			return _toPoint;
		}

		/**
		 * @private
		 */
		public function set toPoint(value:Point):void
		{
			_toPoint = value;
		}

		/**线起点*/
		public function get fromPoint():Point
		{
			return _fromPoint;
		}

		/**
		 * @private
		 */
		public function set fromPoint(value:Point):void
		{
			_fromPoint = value;
		}
		
		/**箭头大小*/
		public function get radius():int
		{
			return _radius;
		}

		public function set radius(value:int):void
		{
			_radius = value;
		}

		/*****************************************************/
		
		
		
	}
}